import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
// This is a servlet class that takes in user id and course code to update the 
// database table course_enrolled.
public class DeleteCourse extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
	response.setContentType("text/html");
	PrintWriter pw = response.getWriter();
	String user_id = request.getParameter("userId");
	String course_code = request.getParameter("courseCode");

	//connect to the database
	Connection con = null;
	PreparedStatement pst = null;
	ResultSet rs = null;
	String query = "";
	try {
							
		con = DriverManager.getConnection("jdbc:mysql://bungle08.cs.toronto.edu:3306/group22",
			"group22", "123456");
					
		if(!con.isClosed()){	
			query = "DELETE FROM course_enrolled WHERE user_name =\""+user_id+"\" and course_code =\""+course_code+"\"";		
			pst = con.prepareStatement(query);
			//pst.setString(1,user_id);
			//pst.setString(2,course_code);
			int i = pst.executeUpdate();
			if(i!=0){
				pw.println("1");
			}			
		}
	}
	 catch(Exception e) {
		pw.println("Exception: " + e.getMessage());
	} finally {
		try {
			if(con != null)
				con.close();
		} catch(SQLException e) {}
	}}
}
					